package com.xm.xmapp;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.ClearEditText;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.RechargeRequest;
import com.xm.xmapp.tools.Tools;

public class CZKCZActivity extends BaseActivity implements OnClickListener {

	private TextView chongzhi;
	private ClearEditText password, kahao;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_czkcz);
	}

	@Override
	public void initView() {

		chongzhi = (TextView) findViewById(R.id.chongzhi);
		password = (ClearEditText) findViewById(R.id.password);
		kahao = (ClearEditText) findViewById(R.id.kahao);
	}

	@Override
	public void initData() {
		setMyTitle("充值卡充值");
	}

	@Override
	public void initEvent() {

		chongzhi.setOnClickListener(this);
		kahao.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

				if (s == null || s.length() == 0)
					return;
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < s.length(); i++) {
					if (i != 4 && i != 9 && i != 14 && s.charAt(i) == ' ') {
						continue;
					} else {
						sb.append(s.charAt(i));
						if ((sb.length() == 5 || sb.length() == 10 || sb.length() == 15)
								&& sb.charAt(sb.length() - 1) != ' ') {
							sb.insert(sb.length() - 1, ' ');
						}
					}
				}

				if (!sb.toString().equals(s.toString())) {
					int index = start + 1;
					if (sb.charAt(start) == ' ') {
						if (before == 0) {
							index++;
						} else {
							kahao.setText(sb.subSequence(0, sb.length() - 1));
							index--;
						}
					} else {
						if (before == 1) {
							index--;
						}
					}
					kahao.setText(sb.toString());
					kahao.setSelection(index);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

		password.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

				if (s == null || s.length() == 0)
					return;
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < s.length(); i++) {
					if (i != 4 && i != 9 && i != 14 && i != 19 && s.charAt(i) == ' ') {
						continue;
					} else {
						sb.append(s.charAt(i));
						if ((sb.length() == 5 || sb.length() == 10 || sb.length() == 15 || sb.length() == 20)
								&& sb.charAt(sb.length() - 1) != ' ') {
							sb.insert(sb.length() - 1, ' ');
						}
					}
				}

				if (!sb.toString().equals(s.toString())) {
					int index = start + 1;
					if (sb.charAt(start) == ' ') {
						if (before == 0) {
							index++;
						} else {
							password.setText(sb.subSequence(0, sb.length() - 1));
							index--;
						}
					} else {
						if (before == 1) {
							index--;
						}
					}
					password.setText(sb.toString());
					password.setSelection(index);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chongzhi:
			if (kahao.getText().toString().replaceAll(" ", "").length() != 15) {
				Tools.showMessage("请输入正确的卡号！");
			} else if (password.getText().toString().replaceAll(" ", "").length() != 18) {
				Tools.showMessage("充值密码错误！");
			} else {
				RechargeRequest requestBean = new RechargeRequest();
				requestBean.prepaid_card_number = kahao.getText().toString().replaceAll(" ", "");
				requestBean.prepaid_card_password = password.getText().toString().replaceAll(" ", "");
				promptDialog.show();
				MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

					@Override
					public void onResponse(String response) {

						promptDialog.dismiss();
						MyAppcation.getMyAppcation().notifyDataSetChanged(MyWalletActivity.class,
								Type.A_MyWallet.GET_WALLET, 0);
					}
				}, new DisssmissInterFace() {

					@Override
					public void putError(VolleyError error) {
						promptDialog.dismiss();
					}
				});
			}
			break;
		default:
			break;
		}
	}

}
